home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / mpeg2decode / src / mpegaudio / decode.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  25KB  |  680 lines

  1. /**********************************************************************
  2. Copyright (c) 1991 MPEG/audio software simulation group, All Rights Reserved
  3. decode.c
  4. **********************************************************************/
  5. /**********************************************************************
  6.  * MPEG/audio coding/decoding software, work in progress              *
  7.  *   NOT for public distribution until verified and approved by the   *
  8.  *   MPEG/audio committee.  For further information, please contact   *
  9.  *   Davis Pan, 508-493-2241, e-mail: pan@3d.enet.dec.com          *
  10.  *                                                                    *
  11.  * VERSION 3.9                                                       *
  12.  *   changes made since last update:                                  *
  13.  *   date   programmers         comment                               *
  14.  * 2/25/91  Douglas Wong,       start of version 1.0 records          *
  15.  *          Davis Pan                                                 *
  16.  * 3/06/91  Douglas Wong        rename: setup.h to dedef.h            *
  17.  *                                      dfilter to defilter           *
  18.  *                                      dwindow to dewindow           *
  19.  *                              integrated "quantizer", "scalefactor" *
  20.  *                              combined window_samples routine into  *
  21.  *                              filter samples                        *
  22.  * 3/31/91  Bill Aspromonte     replaced read_filter by               *
  23.  *                              create_syn_filter and introduced a    *
  24.  *                              new Sub-Band Synthesis routine called *
  25.  *                              SubBandSynthesis()                    *
  26.  * 5/10/91  Vish (PRISM)        Ported to Macintosh and Unix.         *
  27.  *                              Changed "out_fifo()" so that last     *
  28.  *                              unfilled block is also written out.   *
  29.  *                              "create_syn_filter()" was modified so *
  30.  *                              that calculation precision is same as *
  31.  *                              in specification tables.              *
  32.  *                              Changed "decode_scale()" to reflect   *
  33.  *                              specifications.                       *
  34.  *                              Removed all routines used by          *
  35.  *                              "synchronize_buffer()".  This is now  *
  36.  *                              replaced by "seek_sync()".            *
  37.  *                              Incorporated Jean-Georges Fritsch's   *
  38.  *                              "bitstream.c" package.                *
  39.  *                              Deleted "reconstruct_sample()".       *
  40.  * 27jun91  dpwe (Aware)        Passed outFile and &sampFrames as     *
  41.  *                              args to out_fifo() - were global.     *
  42.  *                              Moved "alloc_*" reader to common.c.   *
  43.  *                              alloc, sblimit, stereo passed via new *
  44.  *                              'frame_params struct (were globals).  *
  45.  *                              Added JOINT STEREO decoding, lyrs I&II*
  46.  *                              Affects: decode_bitalloc,buffer_samps *
  47.  *                              Plus a few other cleanups.            *
  48.  * 6/10/91   Earle Jennings     conditional expansion added in        *
  49.  *                              II_dequantize_sample to handle range  *
  50.  *                              problems in MSDOS version             *
  51.  * 8/8/91    Jens Spille        Change for MS-C6.00                   *
  52.  *10/1/91    S.I. Sudharsanan,  Ported to IBM AIX platform.           *
  53.  *           Don H. Lee,                                              *
  54.  *           Peter W. Farrett                                         *
  55.  *10/3/91    Don H. Lee         implemented CRC-16 error protection   *
  56.  *                              newly introduced functions are        *
  57.  *                              buffer_CRC and recover_CRC_error.     *
  58.  * 2/11/92  W. Joseph Carter    Ported new code to Macintosh.  Most   *
  59.  *                              important fixes involved changing     *
  60.  *                              16-bit ints to long or unsigned in    *
  61.  *                              bit alloc routines for quant of 65535 *
  62.  *                              and passing proper function args.     *
  63.  *                              Removed "Other Joint Stereo" option   *
  64.  *                              and made bitrate be total channel     *
  65.  *                              bitrate, irrespective of the mode.    *
  66.  *                              Fixed many small bugs & reorganized.  *
  67.  * 7/27/92  Juan Pineda         Bug fix in SubBandSynthesis()         *
  68.  **********************************************************************/
  69.  
  70. #include        "common.h"
  71. #include        "decoder.h"
  72. #include    "tables/dewindow.h"
  73.  
  74. /***************************************************************
  75. /*
  76. /* This module contains the core of the decoder ie all the
  77. /* computational routines. (Layer I and II only)
  78. /* Functions are common to both layer unless
  79. /* otherwise specified.
  80. /*
  81. /***************************************************************/
  82.  
  83. /*****************************************************************
  84. /*
  85. /* The following routines decode the system information
  86. /*
  87. /****************************************************************/
  88.  
  89. /************ Layer I, Layer II & Layer III ******************/
  90.  
  91. #ifdef STORM
  92. void decode_info(Bit_stream_struc *bs, frame_params *fr_ps)
  93. #else
  94. void decode_info(bs, fr_ps)
  95. Bit_stream_struc *bs;
  96. frame_params *fr_ps;
  97. #endif
  98. {
  99.     layer *hdr = fr_ps->header;
  100.  
  101.     hdr->version = get1bit(bs);
  102.     hdr->lay = 4-getbits(bs,2);
  103.     hdr->error_protection = !get1bit(bs); /* error protect. TRUE/FALSE */
  104.     hdr->bitrate_index = getbits(bs,4);
  105.     hdr->sampling_frequency = getbits(bs,2);
  106.     hdr->padding = get1bit(bs);
  107.     hdr->extension = get1bit(bs);
  108.     hdr->mode = getbits(bs,2);
  109.     hdr->mode_ext = getbits(bs,2);
  110.     hdr->copyright = get1bit(bs);
  111.     hdr->original = get1bit(bs);
  112.     hdr->emphasis = getbits(bs,2);
  113. }
  114.  
  115. /*******************************************************************
  116. /*
  117. /* The bit allocation information is decoded. Layer I
  118. /* has 4 bit per subband whereas Layer II is Ws and bit rate
  119. /* dependent.
  120. /*
  121. /********************************************************************/
  122.  
  123. /**************************** Layer II *************/
  124.  
  125. #ifdef STORM
  126. void II_decode_bitalloc(Bit_stream_struc *bs, unsigned int bit_alloc[2][SBLIMIT], frame_params *fr_ps)
  127. #else
  128. void II_decode_bitalloc(bs, bit_alloc, fr_ps)
  129. Bit_stream_struc *bs;
  130. unsigned int bit_alloc[2][SBLIMIT];
  131. frame_params *fr_ps;
  132. #endif
  133. {
  134.     int i,j;
  135.     int stereo = fr_ps->stereo;
  136.     int sblimit = fr_ps->sblimit;
  137.     int jsbound = fr_ps->jsbound;
  138.     al_table *alloc = fr_ps->alloc;
  139.  
  140.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  141.         bit_alloc[j][i] = (char) getbits(bs,(*alloc)[i][0].bits);
  142.  
  143.     for (i=jsbound;i<sblimit;i++) /* expand to 2 channels */
  144.         bit_alloc[0][i] = bit_alloc[1][i] =
  145.             (char) getbits(bs,(*alloc)[i][0].bits);
  146.  
  147.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  148.         bit_alloc[j][i] = 0;
  149. }
  150.  
  151. /**************************** Layer I *************/
  152.  
  153. #ifdef STORM
  154. void I_decode_bitalloc(Bit_stream_struc *bs, unsigned int bit_alloc[2][SBLIMIT], frame_params *fr_ps)
  155. #else
  156. void I_decode_bitalloc(bs, bit_alloc, fr_ps)
  157. Bit_stream_struc *bs;
  158. unsigned int bit_alloc[2][SBLIMIT];
  159. frame_params *fr_ps;
  160. #endif
  161. {
  162.     int i,j;
  163.     int stereo  = fr_ps->stereo;
  164.     int sblimit = fr_ps->sblimit;
  165.     int jsbound = fr_ps->jsbound;
  166.     int b;
  167.  
  168.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  169.         bit_alloc[j][i] = getbits(bs,4);
  170.     for (i=jsbound;i<SBLIMIT;i++) {
  171.         b = getbits(bs,4);
  172.         for (j=0;j<stereo;j++)
  173.             bit_alloc[j][i] = b;
  174.     }
  175. }
  176.  
  177. /*****************************************************************
  178. /*
  179. /* The following two functions implement the layer I and II
  180. /* format of scale factor extraction. Layer I involves reading
  181. /* 6 bit per subband as scale factor. Layer II requires reading
  182. /* first the scfsi which in turn indicate the number of scale factors
  183. /* transmitted.
  184. /*    Layer I : I_decode_scale
  185. /*   Layer II : II_decode_scale
  186. /*
  187. /****************************************************************/
  188.  
  189. /************************** Layer I stuff ************************/
  190.  
  191. #ifdef STORM
  192. void I_decode_scale(Bit_stream_struc *bs, unsigned int bit_alloc[2][SBLIMIT], unsigned int scale_index[2][3][SBLIMIT], frame_params *fr_ps)
  193. #else
  194. void I_decode_scale(bs, bit_alloc, scale_index, fr_ps)
  195. Bit_stream_struc *bs;
  196. unsigned int bit_alloc[2][SBLIMIT], scale_index[2][3][SBLIMIT];
  197. frame_params *fr_ps;
  198. #endif
  199. {
  200.     int i,j;
  201.     int stereo = fr_ps->stereo;
  202.     int sblimit = fr_ps->sblimit;
  203.  
  204.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  205.         if (!bit_alloc[j][i])
  206.             scale_index[j][0][i] = SCALE_RANGE-1;
  207.         else                    /* 6 bit per scale factor */
  208.             scale_index[j][0][i] =  getbits(bs,6);
  209.  
  210. }
  211.  
  212. /*************************** Layer II stuff ***************************/
  213.  
  214. #ifdef STORM
  215. void II_decode_scale(Bit_stream_struc *bs,unsigned int scfsi[2][SBLIMIT], unsigned int bit_alloc[2][SBLIMIT],unsigned int scale_index[2][3][SBLIMIT], frame_params *fr_ps)
  216. #else
  217. void II_decode_scale(bs,scfsi, bit_alloc,scale_index, fr_ps)
  218. Bit_stream_struc *bs;
  219. unsigned int scfsi[2][SBLIMIT], bit_alloc[2][SBLIMIT],
  220.              scale_index[2][3][SBLIMIT];
  221. frame_params *fr_ps;
  222. #endif
  223. {
  224.     int i,j;
  225.     int stereo = fr_ps->stereo;
  226.     int sblimit = fr_ps->sblimit;
  227.    
  228.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) /* 2 bit scfsi */
  229.         if (bit_alloc[j][i]) scfsi[j][i] = (char) getbits(bs,2);
  230.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++)   
  231.         scfsi[j][i] = 0;
  232.  
  233.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  234.         if (bit_alloc[j][i])   
  235.             switch (scfsi[j][i]) {
  236.                 /* all three scale factors transmitted */
  237.              case 0 : scale_index[j][0][i] = getbits(bs,6);
  238.                 scale_index[j][1][i] = getbits(bs,6);
  239.                 scale_index[j][2][i] = getbits(bs,6);
  240.                 break;
  241.                 /* scale factor 1 & 3 transmitted */
  242.              case 1 : scale_index[j][0][i] =
  243.                  scale_index[j][1][i] = getbits(bs,6);
  244.                 scale_index[j][2][i] = getbits(bs,6);
  245.                 break;
  246.                 /* scale factor 1 & 2 transmitted */
  247.              case 3 : scale_index[j][0][i] = getbits(bs,6);
  248.                 scale_index[j][1][i] =
  249.                     scale_index[j][2][i] =  getbits(bs,6);
  250.                 break;
  251.                 /* only one scale factor transmitted */
  252.              case 2 : scale_index[j][0][i] =
  253.                  scale_index[j][1][i] =
  254.                      scale_index[j][2][i] = getbits(bs,6);
  255.                 break;
  256.                 default : break;
  257.             }
  258.         else {
  259.             scale_index[j][0][i] = scale_index[j][1][i] =
  260.                 scale_index[j][2][i] = SCALE_RANGE-1;
  261.         }
  262.     }
  263.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) {
  264.         scale_index[j][0][i] = scale_index[j][1][i] =
  265.             scale_index[j][2][i] = SCALE_RANGE-1;
  266.     }
  267. }
  268.  
  269. /**************************************************************
  270. /*
  271. /*   The following two routines take care of reading the
  272. /* compressed sample from the bit stream for both layer 1 and
  273. /* layer 2. For layer 1, read the number of bits as indicated
  274. /* by the bit_alloc information. For layer 2, if grouping is
  275. /* indicated for a particular subband, then the sample size has
  276. /* to be read from the bits_group and the merged samples has
  277. /* to be decompose into the three distinct samples. Otherwise,
  278. /* it is the same for as layer one.
  279. /*
  280. /**************************************************************/
  281.  
  282. /******************************* Layer I stuff ******************/
  283.  
  284. #ifdef STORM
  285. void I_buffer_sample(Bit_stream_struc *bs, unsigned int FAR sample[2][3][SBLIMIT], unsigned int bit_alloc[2][SBLIMIT], frame_params *fr_ps)
  286. #else
  287. void I_buffer_sample(bs, sample, bit_alloc, fr_ps)
  288. unsigned int FAR sample[2][3][SBLIMIT];
  289. unsigned int bit_alloc[2][SBLIMIT];
  290. Bit_stream_struc *bs;
  291. frame_params *fr_ps;
  292. #endif
  293. {
  294.     int i,j,k;
  295.     int stereo = fr_ps->stereo;
  296.     int sblimit = fr_ps->sblimit;
  297.     int jsbound = fr_ps->jsbound;
  298.     unsigned int s;
  299.  
  300.     for (i=0;i<jsbound;i++) for (j=0;j<stereo;j++)
  301.         if ( (k = bit_alloc[j][i]) == 0)
  302.             sample[j][0][i] = 0;
  303.         else 
  304.             sample[j][0][i] = (unsigned int) getbits(bs,k+1);
  305.     for (i=jsbound;i<SBLIMIT;i++) {
  306.         if ( (k = bit_alloc[0][i]) == 0)
  307.             s = 0;
  308.         else 
  309.             s = (unsigned int)getbits(bs,k+1);
  310.         for (j=0;j<stereo;j++)
  311.             sample[j][0][i]    = s;
  312.     }
  313. }
  314.  
  315. /*************************** Layer II stuff ************************/
  316.  
  317. #ifdef STORM
  318. void II_buffer_sample(Bit_stream_struc *bs,unsigned int FAR sample[2][3][SBLIMIT],unsigned int bit_alloc[2][SBLIMIT],frame_params *fr_ps)
  319. #else
  320. void II_buffer_sample(bs,sample,bit_alloc,fr_ps)
  321. unsigned int FAR sample[2][3][SBLIMIT];
  322. unsigned int bit_alloc[2][SBLIMIT];
  323. Bit_stream_struc *bs;
  324. frame_params *fr_ps;
  325. #endif
  326. {
  327.     int i,j,k,m;
  328.     int stereo = fr_ps->stereo;
  329.     int sblimit = fr_ps->sblimit;
  330.     int jsbound = fr_ps->jsbound;
  331.     al_table *alloc = fr_ps->alloc;
  332.  
  333.     for (i=0;i<sblimit;i++) for (j=0;j<((i<jsbound)?stereo:1);j++) {
  334.         if (bit_alloc[j][i]) {
  335.             /* check for grouping in subband */
  336.             if ((*alloc)[i][bit_alloc[j][i]].group==3)
  337.                 for (m=0;m<3;m++) {
  338.                     k = (*alloc)[i][bit_alloc[j][i]].bits;
  339.                     sample[j][m][i] = (unsigned int) getbits(bs,k);
  340.                 }         
  341.             else {              /* bit_alloc = 3, 5, 9 */
  342.                 unsigned int nlevels, c=0;
  343.  
  344.                 nlevels = (*alloc)[i][bit_alloc[j][i]].steps;
  345.                 k=(*alloc)[i][bit_alloc[j][i]].bits;
  346.                 c = (unsigned int) getbits(bs, k);
  347.                 for (k=0;k<3;k++) {
  348.                     sample[j][k][i] = c % nlevels;
  349.                     c /= nlevels;
  350.                 }
  351.             }
  352.         }
  353.         else {                  /* for no sample transmitted */
  354.             for (k=0;k<3;k++) sample[j][k][i] = 0;
  355.         }
  356.         if(stereo == 2 && i>= jsbound) /* joint stereo : copy L to R */
  357.             for (k=0;k<3;k++) sample[1][k][i] = sample[0][k][i];
  358.     }
  359.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<stereo;j++) for (k=0;k<3;k++)
  360.         sample[j][k][i] = 0;
  361. }      
  362.  
  363. /**************************************************************
  364. /*
  365. /*   Restore the compressed sample to a factional number.
  366. /*   first complement the MSB of the sample
  367. /*    for layer I :
  368. /*    Use s = (s' + 2^(-nb+1) ) * 2^nb / (2^nb-1)
  369. /*   for Layer II :
  370. /*   Use the formula s = s' * c + d
  371. /*
  372. /**************************************************************/
  373.  
  374. static double c[17] = { 1.33333333333, 1.60000000000, 1.14285714286,
  375.                         1.77777777777, 1.06666666666, 1.03225806452,
  376.                         1.01587301587, 1.00787401575, 1.00392156863,
  377.                         1.00195694716, 1.00097751711, 1.00048851979,
  378.                         1.00024420024, 1.00012208522, 1.00006103888,
  379.                         1.00003051851, 1.00001525902 };
  380.  
  381. static double d[17] = { 0.500000000, 0.500000000, 0.250000000, 0.500000000,
  382.                         0.125000000, 0.062500000, 0.031250000, 0.015625000,
  383.                         0.007812500, 0.003906250, 0.001953125, 0.0009765625,
  384.                         0.00048828125, 0.00024414063, 0.00012207031,
  385.                         0.00006103516, 0.00003051758 };
  386.  
  387. /************************** Layer II stuff ************************/
  388.  
  389. #ifdef STORM
  390. void II_dequantize_sample(unsigned int FAR sample[2][3][SBLIMIT], unsigned int bit_alloc[2][SBLIMIT], double FAR fraction[2][3][SBLIMIT], frame_params *fr_ps)
  391. #else
  392. void II_dequantize_sample(sample, bit_alloc, fraction, fr_ps)
  393. unsigned int FAR sample[2][3][SBLIMIT];
  394. unsigned int bit_alloc[2][SBLIMIT];
  395. double FAR fraction[2][3][SBLIMIT];
  396. frame_params *fr_ps;
  397. #endif
  398. {
  399.     int i, j, k, x;
  400.     int stereo = fr_ps->stereo;
  401.     int sblimit = fr_ps->sblimit;
  402.     al_table *alloc = fr_ps->alloc;
  403.  
  404.     for (i=0;i<sblimit;i++)  for (j=0;j<3;j++) for (k=0;k<stereo;k++)
  405.         if (bit_alloc[k][i]) {
  406.  
  407.             /* locate MSB in the sample */
  408.             x = 0;
  409. #ifndef MS_DOS
  410.             while ((1L<<x) < (*alloc)[i][bit_alloc[k][i]].steps) x++;
  411. #else
  412.             /* microsoft C thinks an int is a short */
  413.             while (( (unsigned long) (1L<<(long)x) <
  414.                     (unsigned long)( (*alloc)[i][bit_alloc[k][i]].steps)
  415.                     ) && ( x < 16) ) x++;
  416. #endif
  417.  
  418.             /* MSB inversion */
  419.             if (((sample[k][j][i] >> x-1) & 1) == 1)
  420.                 fraction[k][j][i] = 0.0;
  421.             else  fraction[k][j][i] = -1.0;
  422.  
  423.             /* Form a 2's complement sample */
  424.             fraction[k][j][i] += (double) (sample[k][j][i] & ((1<<x-1)-1)) /
  425.                 (double) (1L<<x-1);
  426.  
  427.             /* Dequantize the sample */
  428.             fraction[k][j][i] += d[(*alloc)[i][bit_alloc[k][i]].quant];
  429.             fraction[k][j][i] *= c[(*alloc)[i][bit_alloc[k][i]].quant];
  430.         }
  431.         else fraction[k][j][i] = 0.0;   
  432.    
  433.     for (i=sblimit;i<SBLIMIT;i++) for (j=0;j<3;j++) for(k=0;k<stereo;k++)
  434.         fraction[k][j][i] = 0.0;
  435. }
  436.  
  437. /***************************** Layer I stuff ***********************/
  438.  
  439. #ifdef STORM
  440. void I_dequantize_sample(unsigned int FAR sample[2][3][SBLIMIT], double FAR fraction[2][3][SBLIMIT], unsigned int bit_alloc[2][SBLIMIT], frame_params *fr_ps)
  441. #else
  442. void I_dequantize_sample(sample, fraction, bit_alloc, fr_ps)
  443. unsigned int FAR sample[2][3][SBLIMIT];
  444. unsigned int bit_alloc[2][SBLIMIT];
  445. double FAR fraction[2][3][SBLIMIT];
  446. frame_params *fr_ps;
  447. #endif
  448. {
  449.     int i, nb, k;
  450.     int stereo = fr_ps->stereo;
  451.     int sblimit = fr_ps->sblimit;
  452.  
  453.     for (i=0;i<SBLIMIT;i++)
  454.         for (k=0;k<stereo;k++)
  455.             if (bit_alloc[k][i]) {
  456.                 nb = bit_alloc[k][i] + 1;
  457.                 if (((sample[k][0][i] >> nb-1) & 1) == 1) fraction[k][0][i] = 0.0;
  458.                 else fraction[k][0][i] = -1.0;
  459.                 fraction[k][0][i] += (double) (sample[k][0][i] & ((1<<nb-1)-1)) /
  460.                     (double) (1L<<nb-1);
  461.  
  462.                 fraction[k][0][i] =
  463.                     (double) (fraction[k][0][i]+1.0/(double)(1L<<nb-1)) *
  464.                         (double) (1L<<nb) / (double) ((1L<<nb)-1);
  465.             }
  466.             else fraction[k][0][i] = 0.0;
  467. }
  468.  
  469. /************************************************************
  470. /*
  471. /*   Restore the original value of the sample ie multiply
  472. /*    the fraction value by its scalefactor.
  473. /*
  474. /************************************************************/
  475.  
  476. /************************* Layer II Stuff **********************/
  477.  
  478. #ifdef STORM
  479. void II_denormalize_sample(double FAR fraction[2][3][SBLIMIT], unsigned int scale_index[2][3][SBLIMIT],frame_params *fr_ps,int x)
  480. #else
  481. void II_denormalize_sample(fraction, scale_index,fr_ps,x)
  482. double FAR fraction[2][3][SBLIMIT];
  483. unsigned int scale_index[2][3][SBLIMIT];
  484. frame_params *fr_ps;
  485. int x;
  486. #endif
  487. {
  488.     int i,j,k;
  489.     int stereo = fr_ps->stereo;
  490.     int sblimit = fr_ps->sblimit;
  491.  
  492.     for (i=0;i<sblimit;i++) for (j=0;j<stereo;j++) {
  493.         fraction[j][0][i] *= multiple[scale_index[j][x][i]];
  494.         fraction[j][1][i] *= multiple[scale_index[j][x][i]];
  495.         fraction[j][2][i] *= multiple[scale_index[j][x][i]];
  496.     }
  497. }
  498.  
  499. /**************************** Layer I stuff ******************************/
  500.  
  501. #ifdef STORM
  502. void I_denormalize_sample(double FAR fraction[2][3][SBLIMIT],unsigned int scale_index[2][3][SBLIMIT],frame_params *fr_ps)
  503. #else
  504. void I_denormalize_sample(fraction,scale_index,fr_ps)
  505. double FAR fraction[2][3][SBLIMIT];
  506. unsigned int scale_index[2][3][SBLIMIT];
  507. frame_params *fr_ps;
  508. #endif
  509. {
  510.     int i,j,k;
  511.     int stereo = fr_ps->stereo;
  512.     int sblimit = fr_ps->sblimit;
  513.  
  514.     for (i=0;i<SBLIMIT;i++) for (j=0;j<stereo;j++)
  515.         fraction[j][0][i] *= multiple[scale_index[j][0][i]];
  516. }
  517.  
  518. /*****************************************************************
  519. /*
  520. /* The following are the subband synthesis routines. They apply
  521. /* to both layer I and layer II stereo or mono. The user has to
  522. /* decide what parameters are to be passed to the routines.
  523. /*
  524. /***************************************************************/
  525.  
  526. /*************************************************************
  527. /*
  528. /*   Pass the subband sample through the synthesis window
  529. /*
  530. /**************************************************************/
  531.  
  532. /* create in synthesis filter */
  533.  
  534. #ifdef STORM
  535. void create_syn_filter(double FAR filter[64][SBLIMIT])
  536. #else
  537. void create_syn_filter(filter)
  538. double FAR filter[64][SBLIMIT];
  539. #endif
  540. {
  541.     register int i,k;
  542.  
  543.     for (i=0; i<64; i++)
  544.         for (k=0; k<32; k++) {
  545.             if ((filter[i][k] = 1e9*cos((double)((PI64*i+PI4)*(2*k+1)))) >= 0)
  546.                 modf(filter[i][k]+0.5, &filter[i][k]);
  547.             else
  548.                 modf(filter[i][k]-0.5, &filter[i][k]);
  549.             filter[i][k] *= 1e-9;
  550.         }
  551. }
  552.  
  553. /***************************************************************
  554. /*
  555. /*   Window the restored sample
  556. /*
  557. /***************************************************************/
  558.  
  559. /* read in synthesis window */
  560.  
  561. #ifdef STORM
  562. int SubBandSynthesis (double *bandPtr, int channel, short *samples)
  563. #else
  564. int SubBandSynthesis (bandPtr, channel, samples)
  565. double *bandPtr;
  566. int channel;
  567. short *samples;
  568. #endif
  569. {
  570.     register int i,j,k;
  571.     register double *bufOffsetPtr, sum;
  572.     static int init = 1;
  573.     typedef double NN[64][32];
  574.     static NN FAR *filter;
  575.     typedef double BB[2][2*HAN_SIZE];
  576.     static BB FAR *buf;
  577.     static int bufOffset = 64;
  578.     static double FAR *window;
  579.     int clip = 0;               /* count & return how many samples clipped */
  580.  
  581.     if (init) {
  582.         buf = (BB FAR *) mem_alloc(sizeof(BB),"BB");
  583.         filter = (NN FAR *) mem_alloc(sizeof(NN), "NN");
  584.         create_syn_filter(*filter);
  585. #ifdef STORM
  586.         window = (void *)&t_window;
  587. #else
  588.         window = &t_window;
  589. #endif
  590.         bufOffset = 64;
  591.         init = 0;
  592.     }
  593.     if (channel == 0) bufOffset = (bufOffset - 64) & 0x3ff;
  594.     bufOffsetPtr = &((*buf)[channel][bufOffset]);
  595.  
  596.     for (i=0; i<64; i++) {
  597.         sum = 0;
  598.         for (k=0; k<32; k++)
  599.             sum += bandPtr[k] * (*filter)[i][k];
  600.         bufOffsetPtr[i] = sum;
  601.     }
  602.     /*  S(i,j) = D(j+32i) * U(j+32i+((i+1)>>1)*64)  */
  603.     /*  samples(i,j) = MWindow(j+32i) * bufPtr(j+32i+((i+1)>>1)*64)  */
  604.     for (j=0; j<32; j++) {
  605.         sum = 0;
  606.         for (i=0; i<16; i++) {
  607.             k = j + (i<<5);
  608.             sum += window[k] * (*buf) [channel] [( (k + ( ((i+1)>>1) <<6) ) +
  609.                                                   bufOffset) & 0x3ff];
  610.         }
  611.  
  612. /*   {long foo = (sum > 0) ? sum * SCALE + 0.5 : sum * SCALE - 0.5; */
  613.      {long foo = sum * SCALE;
  614.      if (foo >= (long) SCALE)      {samples[j] = SCALE-1; ++clip;}
  615.      else if (foo < (long) -SCALE) {samples[j] = -SCALE;  ++clip;}
  616.      else                           samples[j] = foo;
  617.  }
  618.     }
  619.     return(clip);
  620. }
  621.  
  622. #ifdef STORM
  623. void  buffer_CRC(Bit_stream_struc *bs, unsigned int *old_crc)
  624. #else
  625. void  buffer_CRC(bs, old_crc)
  626. Bit_stream_struc  *bs;
  627. unsigned int  *old_crc;
  628. #endif
  629. {
  630.     *old_crc = getbits(bs, 16);
  631. }
  632.  
  633. #ifdef STORM
  634. void  recover_CRC_error(short FAR pcm_sample[2][3][SBLIMIT], int error_count, frame_params *fr_ps, FILE *outFile, unsigned long *psampFrames)
  635. #else
  636. void  recover_CRC_error(pcm_sample, error_count, fr_ps, outFile, psampFrames)
  637. short FAR pcm_sample[2][3][SBLIMIT];
  638. int error_count;
  639. frame_params *fr_ps;
  640. FILE *outFile;
  641. unsigned long *psampFrames;
  642. #endif
  643. {
  644.     int  stereo = fr_ps->stereo;
  645.     int  num, done, i;
  646.     int  samplesPerFrame, samplesPerSlot;
  647.     layer  *hdr = fr_ps->header;
  648.     long  offset;
  649.     short  *temp;
  650.  
  651.     num = 3;
  652.     if (hdr->lay == 1) num = 1;
  653.  
  654.     samplesPerSlot = SBLIMIT * num * stereo;
  655.     samplesPerFrame = samplesPerSlot * 32;
  656.  
  657.     if (error_count == 1) {     /* replicate previous error_free frame */
  658.         done = 1;
  659.         /* flush out fifo */
  660.         out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  661.         /* go back to the beginning of the previous frame */
  662.         offset = sizeof(short int) * samplesPerFrame;
  663.         fseek(outFile, -offset, SEEK_CUR);
  664.         done = 0;
  665.         for (i = 0; i < SCALE_BLOCK; i++) {
  666.             fread(pcm_sample, 2, samplesPerSlot, outFile);
  667.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  668.         }
  669.     }
  670.     else{                       /* mute the frame */
  671.         temp = (short*) pcm_sample;
  672.         done = 0;
  673.         for (i = 0; i < 2*3*SBLIMIT; i++)
  674.             *temp++ = MUTE;     /* MUTE value is in decoder.h */
  675.         for (i = 0; i < SCALE_BLOCK; i++)
  676.             out_fifo(pcm_sample, num, fr_ps, done, outFile, psampFrames);
  677.     }
  678. }
  679.  
  680.